package com.mengtian_tech.smart_lab.controller.console.system;
import com.mengtian_tech.smart_lab.entity.system.SystemConfig;
import com.mengtian_tech.smart_lab.service.system.SystemConfigService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.springframework.context.annotation.Scope;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.jeecgframework.core.util.MyBeanUtils;

/**   
 *系统配置控制器
 */
@Scope("prototype")
@Controller
@RequestMapping("/systemConfigController")
public class SystemConfigController extends BaseController {
	 
	@Autowired
	private SystemConfigService systemConfigService;
	
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 设备品牌列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(HttpServletRequest request) {
		return new ModelAndView("com/mengtian_tech/smart_lab/system/config/systemConfigList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(SystemConfig config,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(SystemConfig.class, dataGrid); 
		cq.addOrder("configDesc", SortDirection.asc);
		
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, config, request.getParameterMap()); 
		this.systemConfigService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	 
	
	/**
	 * 更新设备品牌
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doUpdate")
	@ResponseBody
	public AjaxJson doUpdate(SystemConfig config, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "更新成功";
		SystemConfig t = systemConfigService.get(config.getId());
		try {
			MyBeanUtils.copyBeanNotNull2Bean(config, t);
			systemConfigService.save(t);
 		} catch (Exception e) {
			e.printStackTrace();
			message = "更新失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	

	 
	/**
	 * 设备品牌编辑页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goUpdate")
	public ModelAndView goUpdate(SystemConfig config, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(config.getId())) {
			config = systemConfigService.get(config.getId());
			req.setAttribute("config", config);
		}
		return new ModelAndView("com/mengtian_tech/smart_lab/system/config/systemConfig-update");
	}
	
	 
}
